<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Enrollment as EnrollmentModel;
use app\admin\model\Student as StudentModel;
use app\admin\model\Course as CourseModel;
use think\Db;

class Enrollment extends BaseController
{
    public function list($q='')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        if($q){
            $pageParam['query']['q'] = $q;
            $student =StudentModel::where('stu_no|stu_name','LIKE',"%$q%")->find();
            $list = $student->enrollments()->order('enrollment_date asc')->paginate(15,false,$pageParam);            
        }else{
            $list = EnrollmentModel::order('enrollment_date asc')->paginate(15);
        }

        //$list = EnrollmentModel::order('enrollment_date asc')->paginate(15);
        $enrollmentCount = EnrollmentModel::count();
        $this->assign('list',$list);
        $this->assign('enrollmentCount',$enrollmentCount);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function export()
    {
        if(request()->isPost())
        {
            //导出数据
            // excel表头  
            $headArr[] = '校区';
            $headArr[] = '年级';
            $headArr[] = '班级';
            $headArr[] = '课程名';
            $headArr[] = '教师名';
            $headArr[] = '课程地点';
            $headArr[] = '课程时间';
            $headArr[] = '学生学号';
            $headArr[] = '学生姓名';
            $headArr[] = '选课时间';
            // 数据
            $list = EnrollmentModel::with('student,course')->select();
            $data = array();
            foreach($list as $enrollment){
                // 获取学生选课数据
                $datatemp[0]=$enrollment->student->school_id === 0 ? '松风':'白马';
                $datatemp[1]=$enrollment->student->grade;
                $datatemp[2]=$enrollment->student->class;
                $datatemp[3]=$enrollment->course->course_name;
                $datatemp[4]=$enrollment->course->teacher;
                $datatemp[5]=$enrollment->course->course_place;
                $datatemp[6]=$enrollment->course->course_date;
                $datatemp[7]=$enrollment->student->stu_no;
                $datatemp[8]=$enrollment->student->stu_name;
                $datatemp[9]=$enrollment->enrollment_date;

                $data[] = $datatemp;
            }
            //dump($data);die;
            $filename = '学生选课信息表';
            if($data){
                exportExcel($filename, $headArr, $data); // $filename excel名称 $headArr excel表头
            }            
        }
        return $this->fetch();
    }

    public function export_no()
    {
        if(request()->isPost()){
            //导出数据
            // excel表头  
            $headArr[] = '校区';
            $headArr[] = '年级';
            $headArr[] = '班级';
            $headArr[] = '学生学号';
            $headArr[] = '学生姓名';
            $headArr[] = '性别';
            // 数据
            $sql = "select s.school_id,s.grade,s.class,s.stu_no,s.stu_name,s.gender from xk_student s left join xk_enrollment e on s.stu_id = e.stu_id where e.enrollment_id is null";
            $list = Db::query($sql);
            //dump($list);die;
            $count = count($list);
            if($count > 0){
                for($i = 0; $i < $count; $i++){
                    $list[$i]["school_id"] = $list[$i]["school_id"] === 0 ? '松风':'白马';
                    $list[$i]["stu_no"] = "'" . $list[$i]["stu_no"];
                    $list[$i]["gender"] = $list[$i]["gender"] === 0 ? '男':'女';
                }            
            }
            $filename = '学生未选课名单';
            if($count > 0){
                exportExcel($filename, $headArr, $list); // $filename excel名称 $headArr excel表头
            }         
        }
           

    }

    //清空数据
    public function empty()
    {
        if(Request()->isPost())
        {
            // 清空数据表
            $result = Db::execute('TRUNCATE table xk_enrollment');
            if ($result===0) {
                return redirect('enrollment/list');
            } else {
                $this->error('操作错误！');
            }
        }

        return $this->fetch();
        
    }

    public function count()
    {
        $courses = CourseModel::with('enrollments')->order('course_id desc')->select();
        if(!$courses)
        {
            return "数据为空!";
        }
        $num = 0;
        foreach($courses as $course){
                // 获取学生选课数据
                $datatemp[0]=++$num;
                $datatemp[1]=$course->course_name;
                $datatemp[2]=$course->teacher;
                $temp = round(count($course->enrollments)/$course->course_number, 2)*100;
                $datatemp[3]=$temp;                
                $datatemp[4]=count($course->enrollments) . '/' . $course->course_number;  
                if($temp<=25){
                    $datatemp[5] = "progress-bar progress-bar-danger";
                    $datatemp[6] = "badge bg-red";
                }
                if($temp>25 && $temp <=50){
                    $datatemp[5] = "progress-bar progress-bar-yellow";
                    $datatemp[6] = "badge bg-yellow";
                }
                if($temp>50 && $temp <=75){
                    $datatemp[5] = "progress-bar progress-bar-primary";
                    $datatemp[6] = "badge bg-light-blue";
                }
                if($temp>75 && $temp <=100){
                    $datatemp[5] = "progress-bar progress-bar-success";
                    $datatemp[6] = "badge bg-green";
                }


                $list[] = $datatemp;
            }
        $enrollmentCount = EnrollmentModel::count();

        $this->assign('list',$list);
        $this->assign('enrollmentCount',$enrollmentCount);
        return $this->fetch();
    }
}